package org.jeecg.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import org.jeecg.modules.baseinfo.service.IYyCompanyInfoService;
import org.jeecg.test.TestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xiaoxiyuan
 * @description 解码器
 * @date 2020-06-11 15:28:41
 */
public class MessageDecoder extends ReplayingDecoder<Void> {
    IYyCompanyInfoService service;
    private ApplicationContext applicationContext;

    public MessageDecoder(ApplicationContext applicationContext){
        this.applicationContext=applicationContext;
    }
    public MessageDecoder(){
    }

    @Override
    public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        System.out.println("MessageDecoder decode 被调用");
        //需要将得到二进制字节码-> MessageProtocol 数据包(对象)
        int length = in.readInt();

        byte[] content = new byte[length];
        in.readBytes(content);
        //service.test(content);
        //封装成 MessageProtocol 对象，放入 out， 传递下一个handler业务处理
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(content);
        System.out.println(content);
        out.add(messageProtocol);
        try {
            service= (IYyCompanyInfoService) applicationContext.getBean("yyCompanyInfoServiceImpl");
            int ii=service.count();
            System.out.println(ii);
        }catch (Exception e){
        e.printStackTrace();
        }


    }
}

